package com.bkgy.modular.tbdsjprocessrulelibrary.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.modular.tbdsjprocessrulelibrary.dao.TbDsjProcessRuleLibraryDao;
import com.bkgy.modular.tbdsjprocessrulelibrary.entity.JTbDsjProcessRuleLibrary;
import com.bkgy.modular.tbdsjprocessrulelibrary.entity.TbDsjProcessRuleLibrary;
import com.bkgy.modular.tbdsjprocessrulelibrary.service.TbDsjProcessRuleLibraryService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* 工艺规则库表接口具体实现类
* @author  生成器生成
* @date 2022-08-01 15:13:18
*/
@Service
public class TbDsjProcessRuleLibraryServiceImpl implements TbDsjProcessRuleLibraryService {
	
	@Autowired
	private TbDsjProcessRuleLibraryDao tbDsjProcessRuleLibraryDao;
	
	/**
     * 添加记录
     * @param data json字符串
     * @param //userId 用户id
     * @param //sname 用户姓名
     */
	public ResultData insertDataTbDsjProcessRuleLibrary(String data){
		try{
			JTbDsjProcessRuleLibrary jtbDsjProcessRuleLibrary = JSON.parseObject(data,JTbDsjProcessRuleLibrary.class);
    		TbDsjProcessRuleLibrary tbDsjProcessRuleLibrary = jtbDsjProcessRuleLibrary.getTbDsjProcessRuleLibrary();
    		//CodiUtil.newRecord(userId,sname,tbDsjProcessRuleLibrary);
            tbDsjProcessRuleLibraryDao.insertDataTbDsjProcessRuleLibrary(tbDsjProcessRuleLibrary);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
    	}catch(Exception e){
    		e.printStackTrace();
    		return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null); 
    	}
	};
	
	/**
	 * 根据主键删除对象
	 * @param indocno  对象主键
	 * */
	public ResultData deleteDataTbDsjProcessRuleLibraryOne(String process_rule_variable){
		try {
            tbDsjProcessRuleLibraryDao.deleteDataTbDsjProcessRuleLibraryOne(process_rule_variable);
             return ResultData.ResultDataSuccessSelf("删除数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 根据主键字符串删除多个对象
     * @param str_id 对象主键字符串
     */
    public ResultData deleteDataTbDsjProcessRuleLibraryMany(String str_id) {
        try {
        	String sql = "delete tb_dsj_process_rule_library where indocno in(" + str_id +")";
            tbDsjProcessRuleLibraryDao.deleteDataTbDsjProcessRuleLibraryMany(sql);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
     * 修改记录
     * @param userId 用户id
     * @param sname 用户姓名
     * @param data sysUser 对象实体
     */
	public ResultData updateDataTbDsjProcessRuleLibrary(String data){
		try {
			JTbDsjProcessRuleLibrary jtbDsjProcessRuleLibrary = JSON.parseObject(data,JTbDsjProcessRuleLibrary.class);
    		TbDsjProcessRuleLibrary tbDsjProcessRuleLibrary = jtbDsjProcessRuleLibrary.getTbDsjProcessRuleLibrary();
        	//CodiUtil.editRecord(userId,sname,tbDsjProcessRuleLibrary);
            tbDsjProcessRuleLibraryDao.updateDataTbDsjProcessRuleLibrary(tbDsjProcessRuleLibrary);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 分页查看记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjProcessRuleLibraryByPage(String data) {
        try {
        	JTbDsjProcessRuleLibrary jtbDsjProcessRuleLibrary = JSON.parseObject(data, JTbDsjProcessRuleLibrary.class); 
        	JSONObject jsonObject  = null;
        	Integer pageIndex = jtbDsjProcessRuleLibrary.getPageIndex();
        	Integer pageSize = jtbDsjProcessRuleLibrary.getPageSize();
        	
        	if(null == pageIndex || null == pageSize){
        		return ResultData.ResultDataFaultSelf("分页参数没有传", null); 
        	}
        	
        	if(null!=jtbDsjProcessRuleLibrary.getCondition()){
    			jsonObject  = JSON.parseObject(jtbDsjProcessRuleLibrary.getCondition().toString());
    		}


			String process_rule_variable = null;
			if (!StringUtils.isEmpty(jsonObject.get("process_rule_variable"))) {
				process_rule_variable = jsonObject.get("process_rule_variable").toString();
			}

			String variable_description = null;
			if (!StringUtils.isEmpty(jsonObject.get("variable_description"))) {
				variable_description = jsonObject.get("variable_description").toString();
			}


    		List<TbDsjProcessRuleLibrary> list = tbDsjProcessRuleLibraryDao.findDataTbDsjProcessRuleLibraryByPage((pageIndex-1)*pageSize, pageIndex*pageSize,process_rule_variable,variable_description);
    		Integer count = tbDsjProcessRuleLibraryDao.findDataTbDsjProcessRuleLibraryByPageSize(process_rule_variable,variable_description);
    		return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
    
    /**
     * 根据主键查询单条记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjProcessRuleLibraryByIndocno(String data) {
        try {
        	JTbDsjProcessRuleLibrary jtbDsjProcessRuleLibrary = JSON.parseObject(data, JTbDsjProcessRuleLibrary.class); 
        	Long indocno = jtbDsjProcessRuleLibrary.getIndocno();
        	
    		TbDsjProcessRuleLibrary tbDsjProcessRuleLibrary = tbDsjProcessRuleLibraryDao.findDataTbDsjProcessRuleLibraryByIndocno(indocno);
    		return ResultData.ResultDataSuccess(tbDsjProcessRuleLibrary);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
	 * 查看记录
	 * @return list 对象集合返回
	 * */
	public List<TbDsjProcessRuleLibrary> findDataTbDsjProcessRuleLibrary(){
		List<TbDsjProcessRuleLibrary> list = tbDsjProcessRuleLibraryDao.findDataTbDsjProcessRuleLibrary();
		return list;
	};
}
